package com.zhb.zhbojbackendcommentservice.controller;

import com.alibaba.nacos.api.model.v2.Result;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhb.zhbojbackendcommentservice.service.CommentService;
import com.zhb.zhbojbackendcommon.common.BaseResponse;
import com.zhb.zhbojbackendcommon.common.ResultUtils;
import com.zhb.zhbojbackendmodel.model.dto.comment.CommentAddRequest;
import com.zhb.zhbojbackendmodel.model.entity.User;
import com.zhb.zhbojbackendmodel.model.vo.CommentVO;
import com.zhb.zhbojbackendserviceclient.service.UserFeignClient;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/")
public class CommentController {

    @Resource
    @Lazy
    private CommentService commentService;
    @Resource
    private UserFeignClient userFeignClient;

    @PostMapping("/add")
    public BaseResponse<Boolean> addComment(@RequestBody CommentAddRequest request, HttpServletRequest httpRequest) {
        User loginUser = userFeignClient.getLoginUser(httpRequest);
        boolean result = commentService.addComment(request, loginUser);
        return ResultUtils.success(result);
    }

    @GetMapping("/list")
    public BaseResponse<Page<CommentVO>> listCommentByQuestionId(
            @RequestParam Long questionId,
            @RequestParam(defaultValue = "1") long pageNum,
            @RequestParam(defaultValue = "10") long pageSize) {
        Page<CommentVO> result = commentService.listCommentByQuestionId(questionId, pageNum, pageSize);
        return ResultUtils.success(result);
    }
}
